package pe.edu.upc.dew.grupo3a.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo3a.model.Administrador;
import pe.edu.upc.dew.grupo3a.model.Cliente;
import pe.edu.upc.dew.grupo3a.model.Pelicula;
import pe.edu.upc.dew.grupo3a.service.AdministradorService;
import pe.edu.upc.dew.grupo3a.service.ClienteService;
import pe.edu.upc.dew.grupo3a.service.PeliculaService;

/**
 * Servlet implementation class ValidaUsuarioServLet
 */
public class ValidaUsuarioServLet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ClienteService clienteService;
	private AdministradorService administradorService;
	private PeliculaService peliculaService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ValidaUsuarioServLet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	@Override
	public void init(ServletConfig config) throws ServletException	{
			this.clienteService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("clienteService", ClienteService.class);
			//
			this.administradorService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("administradorService", AdministradorService.class);
			//
			this.peliculaService = WebApplicationContextUtils
			.getWebApplicationContext(config.getServletContext())
			.getBean("peliculaService", PeliculaService.class);
			
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tipousuario = request.getParameter("cmb_TipoUsuario");
		String dni = request.getParameter("txt_dni");
		String password = request.getParameter("txt_Clave");
		String action = request.getParameter("accion");
		String usuarioactivo =""; 
		HttpSession session = request.getSession();
		Cliente  cliente = null;
		Administrador administrador = null;
		if(tipousuario.equals("Cliente") ){
			if(clienteService.validarAcceso(dni, password)){
				action="OK";
				cliente = clienteService.getClientePorDNI(dni);
				usuarioactivo = cliente.getNombres()+ " " + cliente.getApellidos();
			} else{
				action="NoOK";
			}
				
		} else { 
			if(administradorService.validarAcceso(dni, password)){
				action="OK";
				administrador = administradorService.getAdministradorPorDni(dni);
				usuarioactivo = administrador.getNombres()+ " " + administrador.getApellidos();
			} else{
				action="NoOK";
			}
			
		}
		request.setAttribute("usuarioactivo", usuarioactivo);
		request.setAttribute("dni", dni);
		request.setAttribute("password", password);
		request.setAttribute("tipousuario", tipousuario);
		//
		session.setAttribute("ClienteActivo", cliente);
		session.setAttribute("AdministradorActivo", administrador);
		
		//
		List<Pelicula> peliculasEstreno = peliculaService.getPeliculasPorEstado("Estreno");
		List<Pelicula> peliculasPorEstrenar = peliculaService.getPeliculasPorEstado("Proximamente");
		//
		request.setAttribute("peliculasEstreno", peliculasEstreno);
		request.setAttribute("peliculasPorEstrenar", peliculasPorEstrenar);
		request.setAttribute("accion", action);
		//
		
		//
		request.getRequestDispatcher("Login.jsp").forward(request,response);
	}

}
